#8.3预测鲍鱼的年龄
#从鲍鱼壳的层数推算
import regression
from numpy import *
abX,abY	= regression.loadDataSet('abalone.txt')
#这里用的是局部加权回归
yHat01 = regression.lwlrTest(abX[0:99],abX[0:99],abY[0:99],0.1)
yHat1 = regression.lwlrTest(abX[0:99],abX[0:99],abY[0:99],1)
yHat10 = regression.lwlrTest(abX[0:99],abX[0:99],abY[0:99],10)

#使用计算的结果得出计算的错误情况
err01 = regression.rssError(abY[0:99],yHat01.T)
err1 = regression.rssError(abY[0:99],yHat1.T)
err10 = regression.rssError(abY[0:99],yHat10.T)

print("err01\n",err01);
print("err1\n",err1);
print("err10\n",err10);

#这里用简单的线性回归比较
ws = regression.standRegres(abX[0:99],abY[0:99])
yHat = mat(abX[100:199]) * ws
err = regression.rssError(abY[100:199],yHat.T.A)
print("err\n",err)

###########岭回归##################	
abX , abY = regression.loadDataSet('abalone.txt')
ridgeWeights = regression.ridgeTest(abX,abY)

#绘图
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(ridgeWeights)
#plt.show()

####################前向逐步线性回归###########
xArr , yArr	 = regression.loadDataSet('abalone.txt')
wise = regression.stageWise(xArr, yArr , 0.01 , 200)
print("wise\n",wise);

#与最小而成法进行比较
xMat = mat(xArr)
yMat = mat(yArr).T
xMat = regression.regularize(xMat)
yM = mean(yMat,0)
yMat = yMat - yM
weights = regression.standRegres(xMat,yMat.T)
print(weights.T)
